package com.xin.springcloudconsumer.controller;

import com.xin.springcloudconsumer.dto.UserDTO;
import com.xin.springcloudconsumer.feigns.UserFeign;
import com.xin.springcloudconsumer.form.UserForm;
import com.xin.springcloudconsumer.service.UserService;
import com.xin.springcloudconsumer.utils.SessionValueUtils;
import com.xin.springcloudconsumer.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

/**
 * 调用用户信息模块
 */
@Controller
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 创建用户
     *
     * @param userForm
     * @param bindingResult
     * @param map
     * @return
     */
    @PostMapping("/create")
    public ModelAndView create(@Valid UserForm userForm,
                               BindingResult bindingResult,
                               Map<String, Object> map,
                               HttpServletRequest request) {
        if (bindingResult.hasErrors()) {
            log.info("【创建用户】创建失败!{}", bindingResult.getFieldError().getDefaultMessage());
            String returnUrl = "";
            map.put("returnUrl", returnUrl);
            map.put("message", bindingResult.getFieldError().getDefaultMessage());
            return new ModelAndView("common/msg_error", map);
        }
        String openId = SessionValueUtils.getOpenId(request);
        userForm.setOpenId(openId);
        UserDTO userDTO = userService.create(userForm);

        return new ModelAndView("redirect:/event/findAll");
    }

    /**
     * 更新用户信息
     * @param userForm
     * @param bindingResult
     * @param map
     * @return
     */
    @PostMapping("/update")
    public ModelAndView update(@Valid UserForm userForm,
                               BindingResult bindingResult,
                               Map<String, Object> map) {
        UserDTO userDTO = userService.update(userForm);
        if (ObjectUtils.isEmpty(userDTO)) {
            String returnUrl = "";
            map.put("returnUrl", returnUrl);
            map.put("message", "修改失败!");
            return new ModelAndView("common/msg_error", map);
        }
        return new ModelAndView("redirect:".concat("/user/findOneByUserId").concat("?uid=").concat(userDTO.getUserId()));

    }

    /**
     * 删除用户
     * @param uid
     * @param map
     * @return
     */
    @GetMapping("/delete")
    public ModelAndView delete(@RequestParam(name = "uid",defaultValue = "")String uid,
                               Map<String,Object> map) {
        boolean delete = userService.delete(uid);
        if (!delete){
            String returnUrl = "/event/findAll";
            map.put("returnUrl", returnUrl);
            map.put("message", "删除失败!");
            return new ModelAndView("common/msg_error", map);
        }
        return new ModelAndView("redirect:/event/findAll");
    }

    /**
     * 根据用户Id查询用户(进入用户个人中心)
     *
     * @return
     */
    @GetMapping("/findOneByUserId")
    public ModelAndView findOneByUserId(@RequestParam(name = "uid", defaultValue = "") String uid,
                                        Map<String, Object> map) {
        UserDTO userDTO = userService.findOneByUserId(uid);
        if (ObjectUtils.isEmpty(userDTO)) {
            String returnUrl = "/event/findAll";
            map.put("returnUrl", returnUrl);
            map.put("message", "该用户不存在!");
            return new ModelAndView("common/msg_error", map);
        }
        map.put("userDTO", userDTO);
        //TODO
        return new ModelAndView("", map);
    }

    /**
     * 通过openId查询用户
     * @param map
     * @return
     */
    @GetMapping("/findOneByOpenId")
    public ModelAndView findOneByOpenId(Map<String,Object> map,
                                        HttpServletRequest request){
            //TODO openId
        String openId = SessionValueUtils.getOpenId(request);
        UserDTO userDTO = userService.findOneByOpenId(openId);
        if (ObjectUtils.isEmpty(userDTO)){
               log.info("【查询用户】查询失败!");
            String returnUrl = "";
            map.put("returnUrl", returnUrl);
            map.put("message", "该用户不存在!");
            return new ModelAndView("common/msg_error", map);
        }
        map.put("userDTO",userDTO);
        return new ModelAndView();
    }


}
